/**
 * 话筒发现
 * */


(function () {
})();


//列表
function getList(){
    $.ajax({
        type: apis.findIndex.method,
        url: apis.findIndex.url,
        data: {},
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            var d = res.data;
            var h = '', i;
            for (i = 0; i < d.length; i ++) {
                h += '<tr class="text-c">';
                h += '<td>' + d[i].id + '</td>';
                h += '<td>' + d[i].title + '</td>';
                h += '<td>' + d[i].type_name + '</td>';
                h += '<td>' + d[i].uname + '</td>';
                h += '<td>' + d[i].status_name + '</td>';
                h += '<td>' + d[i].addtime + '</td>';
                h += '<td>' + d[i].uptime + '</td>';
                h += '<td class="f-14">';
                h += '<a href="javascript:;" onclick="find_show(\'详情\',\'find-show.html\', ' + d[i].id + ');">';
                h += '详情<i class="Hui-iconfont">&#xe6df;</i>';
                h += '</a>';
                if (d[i].status == 0) {
                    h += ' &nbsp; ';
                    h += '<a href="javascript:;" onclick="auth_yes(' + d[i].id + ');">';
                    h += '审核通过';
                    h += '</a>';
                }
                h += ' &nbsp; ';
                if (d[i].top == 0) {
                    h += '<a href="javascript:;" onclick="top_yes(' + d[i].id + ');" style="color: green;">';
                    h += '置顶';
                    h += '</a>';
                } else {
                    h += '<a href="javascript:;" onclick="top_no(' + d[i].id + ');" style="color: red;">';
                    h += '取消置顶';
                    h += '</a>';
                }
                h += ' &nbsp; ';
                h += '<a href="javascript:;" onclick="find_del(' + d[i].id + ');" style="color: red;">';
                h += '删除<i class="Hui-iconfont">&#xe6e2;</i>';
                h += '</a>';
                h += '</td>';
                h += '</tr>';
            }
            $('tbody').html(h);
            $('#pageTotal').html(d.length);
            pages.total = res.total;
            pageHtml(pages);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//审核通过
function auth_yes(id){
    var msg = '确认要 <b style="color: green; font-size: 20px;">通过</b> 吗？';
    layer.confirm(msg,function(index){
        $.ajax({
            type: apis.findAuth.method,
            url: apis.findAuth.url,
            data: { id: id },
            success: function (res) {
                console.log(res);
                if (res.code < 0) {
                    msgPop({msg: res.msg}); return
                }
                layer.msg('操作成功!',{icon: 1,time: 1000});
                setTimeout(function(){
                    top.location.reload();
                }, 1000);
            },
            error: function () {
                console.log('错误了！');
            }
        });
    });
}


//详情弹窗
function find_show(title,url,id,w,h){
    url += '?id=' + id;
    layer_show(title,url,w,h);
}


//详情
function showOne(){
    $.ajax({
        type: apis.findOne.method,
        url: apis.findOne.url,
        data: { id: id },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            var d = res.data;
            var h = '', i;
            $('#title').html(d.title);
            $('#tname').html(d.type_name);
            $('#cnt').html(d.content);
            if (d.imgList.length > 0) {
                h = '';
                for (i = 0; i < d.imgList.length; i ++) {
                    if (d.imgList[i] != '') {
                        h += '<img src="' + d.imgList[i] + '"/>';
                    }
                }
                $('#imgOut').html(h);
            }
            $('#puber').html(d.puber);
            $('#pubtime').html(d.addtime);
            $('#status_name').html(d.status_name);
            $('#status_time').html(d.uptime);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//删除
function find_del(id){
    var msg = '确认要 <b style="color: red; font-size: 20px;">删除</b> 吗？';
    layer.confirm(msg,function(index){
        $.ajax({
            type: apis.findDel.method,
            url: apis.findDel.url,
            data: { id: id },
            success: function (res) {
                console.log(res);
                if (res.code < 0) {
                    msgPop({msg: res.msg}); return
                }
                layer.msg('已删除!',{icon: 1,time: 1000});
                setTimeout(function(){
                    top.location.reload();
                }, 1000);
            },
            error: function () {
                console.log('错误了！');
            }
        });
    });
}


//置顶
function top_yes(v){
    var msg = '确认要 <b style="color: green; font-size: 20px;">置顶</b> 吗？';
    layer.confirm(msg,function(index){
        setTop({id: v, top: 1});
    });
}
//取消置顶
function top_no(v){
    var msg = '确认要 <b style="color: red; font-size: 20px;">取消置顶</b> 吗？';
    layer.confirm(msg,function(index){
        setTop({id: v, top: 0});
    });
}
//置顶接口
function setTop(e){
    var top_name = e.top==0 ? '取消' : '置顶';
    $.ajax({
        type: apis.findTop.method,
        url: apis.findTop.url,
        data: e,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            layer.msg(top_name + '成功!',{icon: 1,time: 1000});
            setTimeout(function(){
                top.location.reload();
            }, 1000);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//页面开关
function switchOne(){
    $.ajax({
        type: apis.findSwitchOne.method,
        url: apis.findSwitchOne.url,
        data: {},
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            var d = res.data;
            var h = '';
            if (d == 1) {
                h = '<a href="javascript:;" class="btn btn-primary radius" style="background: red;" onclick="switchNo();">页面关闭</a>';
            } else {
                h = '<a href="javascript:;" class="btn btn-primary radius" onclick="switchYes();">页面开启</a>';
            }
            $('#switch').html(h);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}
//页面开关接口
function switchYes(){
    switchModify(1);
}
function switchNo(){
    switchModify(0);
}
function switchModify(v){
    $.ajax({
        type: apis.findSwitchModify.method,
        url: apis.findSwitchModify.url,
        data: { kg: v },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            layer.msg('操作成功!',{icon: 1,time: 1000});
            setTimeout(function(){
                top.location.reload();
            }, 1000);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}